$(function () {
    $('form').parsley();
    dynamicBinding();
    getAllLaws();
});

//遍历所有laws输出到表格
function getAllLaws() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {
            title: "序号", data: null, "render": function (data, type, row, meta) {
                return data = Number(meta.row) + Number(1);
            }
        },
        {title: "法律法规", data: "lawsAndRegulations"},
        {title: "标准规范", data: "standardSpecification"},
        {title: "管理文件名称", data: "fileName"},
        {
            title: "文件地址", data: "fileHref", "render": function (data, type, row, meta) {
                let index = data.lastIndexOf("/");
                return '<a href="' + data + '" download="' + data.substring(index + 1, data.length) + '" target="_blank">' + data.substring(index + 1, data.length) + '</a>';
            }
        },
        {
            title: "操作", data: "id", "orderable": false, // 禁用排序
            "defaultContent": "", "render": function (data, type, row, meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateLawsModal'>更新</button>";
                }
                return data = deleteStr + updateStr;
            }
        }
    ];
    let url = 'laws/getAllLaws';
    dataTableInit(url, columns);
}


//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/laws/deleteLaws');
    //为所有的update按钮绑定update事件
    updateBind('#toUpdateLaws', '/laws/updateLaws', false, false, "#updateLawsModal");
    //为所有的add按钮绑定add事件
    addBind('#toAddLaws', '/laws/addLaws', false, false, "#addLawsModal");
    //为add模态框绑定消失事件-清空模态框中内容

    $('#addLawsModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddLaws").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateLawsModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateLaws").reset();
    });
}
